#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

//函数对象
struct CompareList
{
    bool operator()( const int& lhs, const int& rhs ) const
    {
        cout << "bool CompareList" << endl;
        return lhs > rhs;
    }
};
void test()
{
    list<int> number = {1, 3, 5, 9, 8, 5, 5, 6, 2, 5};
    display(number);

    cout << endl << "测试list的unique函数" << endl;
    number.unique();
    display(number);

    cout << endl << "将list逆置" << endl;
    number.reverse();
    display(number);

    cout << endl << "将list排序" << endl;
    number.sort();
    /* number.sort(std::less<int>()); */
    /* number.sort(std::greater<int>()); */
    /* number.sort(CompareList()); */
    display(number);

    cout << endl << "测试list的unique函数" << endl;
    number.unique();
    display(number);

    cout << endl << "测试list的merge函数"<< endl;
    list<int> listNum = {11, 44, 88, 22, 7};
    /* listNum.sort(std::greater<int>()); */
    listNum.sort();
    display(listNum);
    /* number.sort(std::greater<int>()); */
    number.merge(listNum);
    display(number);
    display(listNum);

    cout << endl << "测试list的splice操作" << endl;
    auto it = number.begin();
    ++it;
    cout << "*it = " << *it << endl;
    list<int> number2 = {123, 456, 789, 15, 77};
    /* number.splice(it, number2); */
    /* display(number); */
    /* display(number2); */

    cout << endl;
    auto it2 = number2.begin();
    cout << "*it2 = " << *it2 << endl;
    number.splice(it, number2, it2);
    display(number);
    display(number2);

    cout << endl << endl;
    auto it3 = number2.end();
    --it3;
    it2 = number2.end();
    --it2;
    --it2;
    --it2;
    cout << "*it = " << *it << endl;
    cout << "*it2 = " << *it2 << endl;
    cout << "*it3 = " << *it3 << endl;
    number.splice(it, number2, it2, it3);
    display(number);
    display(number2);
}

void test2()
{
    list<int> number = {1, 3, 5, 8, 9, 11, 33, 44, 99};
    display(number);

    cout << endl << endl;
    auto it = number.end();
    --it;
    --it;
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    auto it2 = number.begin();
    ++it2;
    cout << "*it2 = " << *it2 << endl;

    number.splice(it2, number, it);
    display(number);
}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}

